/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cart;

import java.util.ArrayList;
import java.util.List;
import model.Order;
import model.Orderitem;

/**
 *
 * @author Thai Ha Cong
 */
public class ShoppingCart {
    private List<ShoppingCartItem> shoppingCartItemList;

    public ShoppingCart() {
        shoppingCartItemList = new ArrayList<ShoppingCartItem>();
    }

    public ShoppingCart(List<ShoppingCartItem> shoppingCartItemList) {
        this.shoppingCartItemList = shoppingCartItemList;
    }

    public List<ShoppingCartItem> getShoppingCartItemList() {
        return shoppingCartItemList;
    }

    public void setShoppingCartItemList(List<ShoppingCartItem> shoppingCartItemList) {
        this.shoppingCartItemList = shoppingCartItemList;
    }
    
    public void addItem(ShoppingCartItem sci){
        if (shoppingCartItemList.contains(sci)) {
            ShoppingCartItem existedItem = shoppingCartItemList.get(shoppingCartItemList.indexOf(sci));
            existedItem.setQuantity(existedItem.getQuantity() + sci.getQuantity());
        } else {
            shoppingCartItemList.add(sci);
        }
    }
    
    public boolean removeItem(ShoppingCartItem sci){
        boolean result;
        
        if (shoppingCartItemList.contains(sci)) {
            shoppingCartItemList.remove(sci);
            result = true;
        } else {
            result = false;
        }
        
        return result;
    }
    
    public double getTotalAmount(){
        double result = 0;
        
        for (ShoppingCartItem sci:shoppingCartItemList) {
            result = result + sci.getPrice() * sci.getQuantity();
        }
        
        return result;
    }
    
    public int getTotalQuantity(){
        int result = 0;
        
        for (ShoppingCartItem sci:shoppingCartItemList) {
            result = result + sci.getQuantity();
        }
        
        return result;
    }
    
    public String getDescription() {
        String result = "";
        
        for (ShoppingCartItem sci:shoppingCartItemList) {
            result = result + sci.getProduct().getName() + " - Qty: " + sci.getQuantity() + " - Unit Price: " + String.format("%1$,.0f", (sci.getPrice() * 22000)) + " đ<br>";
        }
        
        return result;
    }
    
    public Order convertToOrder() {
        Order order = new Order();
        
        order.setTotalAmount(this.getTotalAmount());
        order.setTotalQuantity(this.getTotalQuantity());
        
        List<Orderitem> orderitems = new ArrayList<Orderitem>();
        for (ShoppingCartItem sci:shoppingCartItemList) {
            Orderitem oi = new Orderitem(sci.getQuantity(), sci.getPrice(), sci.getProduct());
            orderitems.add(oi);
        }
        
        order.setOrderitemList(orderitems);
        
        return order;
    }
}
